package com.dal.rpc.core.serializer;

import com.dal.rpc.core.serializer.json.JsonSerializer;
import com.dal.rpc.core.serializer.kryo.KryoSerializer;

/**
 * @program: dal-springboot
 * @description: 序列化接口
 * @author: jian
 * @create: 2021-11-17 12:35
 */
public interface Serializer {
    /**
     * 序列化
     * @param obj
     * @return
     */
    byte[] serialize(Object obj);

    /**
     * 反序列化
     * @param bytes
     * @param clazz
     * @return
     */
    Object deserialize(byte[] bytes, Class<?> clazz);

    /**
     * 序列化状态码
     * @return
     */
    int getCode();

    static Serializer getByCode(int code) {
        switch (code) {
            case 1:
                return new JsonSerializer();
            case 0:
                return new KryoSerializer();
            default:
                return null;
        }
    }
}
